package wecom

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

// Notifier 企业微信通知器
type Notifier struct {
	webhookURL string
}

// NewNotifier 创建通知器
func NewNotifier(webhookURL string) *Notifier {
	return &Notifier{
		webhookURL: webhookURL,
	}
}

// Send 发送消息
func (n *Notifier) Send(content string) error {
	msg := NewTextMessage(content)

	data, err := json.Marshal(msg)
	if err != nil {
		return fmt.Errorf("marshal message failed: %v", err)
	}

	resp, err := http.Post(n.webhookURL, "application/json", bytes.NewBuffer(data))
	if err != nil {
		return fmt.Errorf("send message failed: %v", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("send message failed with status code: %d", resp.StatusCode)
	}

	return nil
}
